var prefix = ctx + "md/business"
$().ready(function() {
	validateRule();
	//loadTableId();
	loadFieldId();
	loadLinkWay();
	loadDataSourceType();
	loadBusiUse();
	loadAlgorithmType();
	loadOpenLevel();
	loadType();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$('#dataUseWarning').hide();
	var dataType = $('#dataType').val();
	var algorithm = $('#algorithmType').val();
	var linkWay = $('#linkWay').val();
	var dataUse = $("input:checkbox[name='dataUse']:checked").map(function(index,elem) {
        return $(elem).val();
    }).get().join(',');
	var ok = true;
	if(algorithm == 1){
		var tableId = $('#tableId').val();
		getTableType(tableId);
	} else if(algorithm == 2) {
		var tableId = $('#stTableId').val();
		getTableType(tableId);
	} else if(algorithm == 5) {
		var tableId = $('#sTableId').val();
		getTableType(tableId);
	}
	
	//如果表不匹配
	if(dataType == '0'){//法人
		if(linkWay == 1){//法人和法人
			ok = false;
			parent.layer.alert("数据来源和连接方式不匹配");
		} else if(linkWay == 2 && tableType != 4){//法人和自然人
			ok = false;
			parent.layer.alert("数据来源和连接方式不匹配");
		}
	}else if(dataType == '1'){//自然人
		if(linkWay == 1 && tableType != 4){//自然人和法人
			ok = false;
			parent.layer.alert("数据来源和连接方式不匹配");
		} else if(linkWay == 2 &&  tableType != 5){//自然人和自然人
			ok = false;
			parent.layer.alert("数据来源和连接方式不匹配");
		}
	}
	//如果用途为空...
	if(dataUse == null || dataUse == ""){
		//$('#dataUseWarning').show();
		parent.layer.alert("请至少选择一种用途");
		ok = false;
	}
	if(ok){
	if(algorithm == '1'){
		$.ajax({
			cache : true,
			type : "POST",
			url : prefix + "/save",
			dataType: "json",
			data : {busiName:$('#busiName').val(),
					dataType:$('#dataType').val(),
					busiType:$('#busiType').val(),
					busiTypeName:$("#busiType option:checked").text(),
					busiTypeNode:$('#busiTypeNode').val(),
					busiTypeNodeName:$("#busiTypeNode option:checked").text(),
					openLevel:$('#openLevel').val(),
					algorithmType:$('#algorithmType').val(),
					linkWay:$('#linkWay').val(),
					dataUse:dataUse,
					tableId:$('#tableId').val(),
					columnId:$('#fieldId').val(),
					busiDesc:$('#busiDesc').val()
					},
			async : false,
			error : function(request) {
				parent.layer.alert("网络超时");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg("操作成功");
					parent.reLoad();
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);

				} else {
					parent.layer.alert(data.msg)
				}

			}
		});
	}else if(algorithm == '2'){
		if(linkWay == '0' || linkWay == '1' || linkWay == '2'){
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/save",
				dataType: "json",
				data : {busiName:$('#busiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						tableId:$('#stTableId').val(),
						detColumnId:$('#stDetFieldId').val(),
						busiDesc:$('#stBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);

					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}else{
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/save",
				dataType: "json",
				data : {busiName:$('#busiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						tableId:$('#stTableId').val(),
						busiDesc:$('#stBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);

					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}
		
	}else if(algorithm == '3'){
		if(linkWay == '0' || linkWay == '1' || linkWay == '2'){//链接方式为明细/法人信用记录/自然人信用记录
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/save",
				dataType: "json",
				data : {busiName:$('#busiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						dataSource:$('#dataSource').val(),
						busiSql:$('#busiSql').val(),
						busiDesc:$('#cBusiDesc').val(),
						busiSqldet:$('#busiSqldet').val(),
						busiDescdet:$('#busiDescdet').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}else{
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/save",
				dataType: "json",
				data : {busiName:$('#busiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						dataSource:$('#dataSource').val(),
						busiSql:$('#busiSql').val(),
						busiDesc:$('#cBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}
	}else if(algorithm == '4'){
		$.ajax({
			cache : true,
			type : "POST",
			url : prefix + "/save",
			dataType: "json",
			data : {busiName:$('#busiName').val(),
					dataType:$('#dataType').val(),
					busiType:$('#busiType').val(),
					busiTypeName:$("#busiType option:checked").text(),
					busiTypeNode:$('#busiTypeNode').val(),
					busiTypeNodeName:$("#busiTypeNode option:checked").text(),
					openLevel:$('#openLevel').val(),
					algorithmType:$('#algorithmType').val(),
					linkWay:$('#linkWay').val(),
					dataUse:dataUse,
					springBean:$('#springBean').val(),
					interfaceName:$('#interfaceName').val(),
					busiDesc:$('#iBusiDesc').val(),
					busiDesc:$('#iBusiDesc').val()
					},
			async : false,
			error : function(request) {
				parent.layer.alert("网络超时");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg("操作成功");
					parent.reLoad();
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);

				} else {
					parent.layer.alert(data.msg)
				}

			}
		});
	}else if(algorithm == '5'){
		if(linkWay == '0' || linkWay == '1' || linkWay == '2'){//链接方式为明细/法人信用记录/自然人信用记录
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/save",
				dataType: "json",
				data : {busiName:$('#busiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						tableId:$('#sTableId').val(),
						columnId:$('#sFieldId').val(),
						columnValue:$('#fieldValue').val(),
						detColumnId:$('#sDetFieldId').val(),
						busiDesc:$('#sBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);

					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		} else {
			$.ajax({
				cache : true,
				type : "POST",
				url : prefix + "/save",
				dataType: "json",
				data : {busiName:$('#busiName').val(),
						dataType:$('#dataType').val(),
						busiType:$('#busiType').val(),
						busiTypeName:$("#busiType option:checked").text(),
						busiTypeNode:$('#busiTypeNode').val(),
						busiTypeNodeName:$("#busiTypeNode option:checked").text(),
						openLevel:$('#openLevel').val(),
						algorithmType:$('#algorithmType').val(),
						linkWay:$('#linkWay').val(),
						dataUse:dataUse,
						tableId:$('#sTableId').val(),
						columnId:$('#sFieldId').val(),
						columnValue:$('#fieldValue').val(),
						busiDesc:$('#sBusiDesc').val()
						},
				async : false,
				error : function(request) {
					parent.layer.alert("网络超时");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);

					} else {
						parent.layer.alert(data.msg)
					}

				}
			});
		}
	}
	}
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			busiName : {
				required : true
			}
		},
		messages : {
			busiName : {
				required : icon + "请输入业务元名称"
			}
			
		},
	})
}
function loadOpenLevel(){
	var html = "";
	$.ajax({
		url : ctx + 'md/business/byValue',
		data : {
			'value' : 'open_level'
		},
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
			}
			$('#openLevel').append(html);
		}
	});
}

function loadType(){
	$('#dataType').on('change', function() {
			$("#busiTypeNode").html('<option value="">--请选择业务小类--</option>');
			var dataType = $('#dataType').val();
			var value = "";
			if(dataType == '0'){
				value =	'legal_person';
			}else if(dataType == '1'){
				value =	'natural_person';
			}
			var html = "";
			$.ajax({
				url : ctx + 'md/business/byValue',
				data : {
					'value' : value
				},
				success : function(data) {
					$("#busiType").html('<option value="">--请选择业务大类--</option>');
					//加载数据
					for (var i = 0; i < data.length; i++) {
						html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
						
					}
					$('#busiType').append(html);
					//点击事件
					$('#busiType').on('change', function() {
						var a = $('#busiType').val();
						loadTypeNode(a);
					});
				}
			});
			$("#tableId").html('<option value="">--请选择表--</option>');
			$("#stTableId").html('<option value="">--请选择表--</option>');
			$("#sTableId").html('<option value="">--请选择表--</option>');
			$("#fieldId").html('<option value="">--请选择字段--</option>');
			$("#sFieldId").html('<option value="">--请选择字段--</option>');
			$('#stDetFieldName').val("");
			$('#stDetFieldId').val("");
			$('#sDetFieldName').val("");
			$('#sDetFieldId').val("");
			loadTableId();
		}	
	);
}

function loadTypeNode(parentId){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/busiTypeNode',
		data : {
			'parentId' : parentId
		},
		success : function(data) {
			$("#busiTypeNode").html('<option value="">--请选择业务小类--</option>');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
				
			}
			$("#busiTypeNode").append(html);
		}
	});
}
/**
 * 根据算法类型加载页面
 */
function loadAlgorithmType(){
	$('#algorithmType').on('change', function() {
		var algorithmType = $('#algorithmType').val();
		if(algorithmType == "1"){
			$('#simple').show();
			$('#simpleTable').hide();
			$('#customize').hide();
			$('#interface').hide();
			$('#splitStatistics').hide();
		}else if(algorithmType == "2"){
			$('#simple').hide();
			$('#simpleTable').show();
			$('#customize').hide();
			$('#interface').hide();
			$('#splitStatistics').hide();
		}else if(algorithmType == "3"){
			$('#simple').hide();
			$('#simpleTable').hide();
			$('#customize').show();
			$('#interface').hide();
			$('#splitStatistics').hide();
		}else if(algorithmType == "4"){
			$('#simple').hide();
			$('#simpleTable').hide();
			$('#customize').hide();
			$('#interface').show();
			$('#splitStatistics').hide();
		}else if(algorithmType == "5"){
			$('#simple').hide();
			$('#simpleTable').hide();
			$('#customize').hide();
			$('#interface').hide();
			$('#splitStatistics').show();
		}
	});
}
/**
 * 加载用途下拉框
 */
function loadBusiUse(){
	var html = "";
	$.ajax({
		url : ctx + 'md/business/byValue',
		data : {
			'value' : 'busi_use'
		},
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<label class="control-label" style="font-weight: normal;"><input type="checkbox" id="dataUse" name="dataUse" value="' + data[i].value + '">' + data[i].name + '&nbsp;&nbsp;&nbsp;&nbsp;</label>';
			}
			$('#dataUseDiv').append(html);
			$('#dataUseDiv').append('<span style="display: none;color: #cc5965;" id="dataUseWarning"><i class="fa fa-times-circle"></i>至少选择一项用途</span>');
		}
	});
}
/**
 * 加载数据源下拉框
 */
function loadDataSourceType(){
	var html = "";
	$.ajax({
		url : ctx + 'md/source/getAllSource',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].sourceId + '">' + data[i].sourceName + '</option>'
				
			}
			$('#dataSource').append(html);
		}
	});
}
/**
 * 加载连接方式下拉框
 */
function loadLinkWay(){
	$('#linkWay').on('change', function() {
		var linkWay = $('#linkWay').val();
		if(linkWay == '0' || linkWay == '1' || linkWay == '2'){
			$('#detailDiv').show();
			$('#detailDiv1').show();
			$('#detailDiv2').show();
		}else{
			$('#detailDiv').hide();
			$('#detailDiv1').hide();
			$('#detailDiv2').hide();
		}
	});
}
/**
 * 加载表下拉框
 */
function loadTableId(){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/getTable',
		data : {
			dataType : $("#dataType").val()
		},
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].tableId + '">' + data[i].tableName +((data[i].tableComment == null || data[i].tableComment == "")?"":" ("+data[i].tableComment+") ")+  '</option>'
				
			}
			$("#tableId").append(html);
			$("#stTableId").append(html);
			$("#sTableId").append(html);
			//点击事件
			$('#tableId').on('change', function() {
				var a = $('#tableId').val();
				loadFieldId(a);
			});
			$('#stTableId').on('change', function() {
				var a = $('#stTableId').val();
				loadFieldId(a);
				$('#stDetFieldName').val("");
				$('#stDetFieldId').val("");
			});
			$('#sTableId').on('change', function() {
				var a = $('#sTableId').val();
				loadFieldId(a);
				$('#sDetFieldName').val("");
				$('#sDetFieldId').val("");
			});
		}
	});
}
/**
 * 加载字段下拉框
 */
function loadFieldId(tableId){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/getField',
		data : {
			'tableId' : tableId
		},
		success : function(data) {
			$("#fieldId").html('<option value="">--请选择字段--</option>');
			$("#sFieldId").html('<option value="">--请选择字段--</option>');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].columnId + '">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'		
			}
			$("#fieldId").append(html);
			$("#sFieldId").append(html);
		}
	});
}

function toDetField(){
	var algorithmType = $('#algorithmType').val();
	if(algorithmType == 2){//算法类型为单表统计
		var tableId = $('#stTableId').val();
	}else if(algorithmType == 5){//算法类型为拆分统计
		var tableId = $('#sTableId').val();
	}
	if(tableId != null && tableId != ""){
		var page = layer.open({
			type : 2,
			title : '明细字段',
			maxmin : true,
			shadeClose : false, // 点击遮罩关闭层
			area : [ '800px', '520px' ],
			content : prefix + '/toDetField/' + tableId // iframe的url
		});
	}else{
		parent.layer.alert("请选择数据源");
	}
}
/**
 * 获取所选表类型
 * @param tableId
 */
function getTableType(tableId){
	$.ajax({
		url : ctx + 'md/business/getTableType',
		contentType : 'text',
		async : false,
		data : {
			'tableId' : tableId
		},
		success : function(data) {
			window.tableType = data;
		}
	});
}



